package com.industrial.premu.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.industrial.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.premu.domain.PreMuIndustryCoefficient;
import com.industrial.premu.service.IPreMuIndustryCoefficientService;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.page.TableDataInfo;

/**
 * 行业系数Controller
 * 
 * @author ZhouS
 * @date 2024-01-23
 */
@RestController
@RequestMapping("/coefficient")
public class PreMuIndustryCoefficientController extends BaseController
{
    @Autowired
    private IPreMuIndustryCoefficientService preMuIndustryCoefficientService;

    /**
     * 查询行业系数列表
     */
    @RequiresPermissions("premu:coefficient:list")
    @GetMapping("/list")
    public TableDataInfo list(PreMuIndustryCoefficient preMuIndustryCoefficient)
    {
        startPage();
        List<PreMuIndustryCoefficient> list = preMuIndustryCoefficientService.selectPreMuIndustryCoefficientList(preMuIndustryCoefficient);
        return getDataTable(list);
    }

    /**
     * 导出行业系数列表
     */
    @RequiresPermissions("premu:coefficient:export")
    @Log(title = "行业系数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreMuIndustryCoefficient preMuIndustryCoefficient)
    {
        List<PreMuIndustryCoefficient> list = preMuIndustryCoefficientService.selectPreMuIndustryCoefficientList(preMuIndustryCoefficient);
        ExcelUtil<PreMuIndustryCoefficient> util = new ExcelUtil<PreMuIndustryCoefficient>(PreMuIndustryCoefficient.class);
        util.exportExcel(response, list, "行业系数数据");
    }

    /**
     * 获取行业系数详细信息
     */
    @RequiresPermissions("premu:coefficient:query")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(preMuIndustryCoefficientService.selectPreMuIndustryCoefficientByBaseId(baseId));
    }

    /**
     * 新增行业系数
     */
    @RequiresPermissions("premu:coefficient:add")
    @Log(title = "行业系数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PreMuIndustryCoefficient preMuIndustryCoefficient)
    {
        String district= SecurityUtils.getLoginUser().getSysUser().getDistrict();
        PreMuIndustryCoefficient preMuIndustryCoefficientNew =new PreMuIndustryCoefficient();
        preMuIndustryCoefficientNew.setCounty(district);
        preMuIndustryCoefficientNew.setIndustry(preMuIndustryCoefficient.getIndustry());
        preMuIndustryCoefficientNew.setYears(preMuIndustryCoefficient.getYears());
        preMuIndustryCoefficientNew.setEnterType(preMuIndustryCoefficient.getEnterType());
        List<PreMuIndustryCoefficient> list = preMuIndustryCoefficientService.selectPreMuIndustryCoefficientList(preMuIndustryCoefficient);
        if(list.size()>0){
            return warn("该区县的行业已添加！");
        }else{
            preMuIndustryCoefficient.setCounty(district);
            double totle=0;
            if(preMuIndustryCoefficient.getPreMuIndustryValuesList()!=null){

                for (int i = 0; i <preMuIndustryCoefficient.getPreMuIndustryValuesList().size() ; i++) {
                    totle+=Double.valueOf(preMuIndustryCoefficient.getPreMuIndustryValuesList().get(i).getIndicatorValues());
                }
            }
            if(totle>100){
                return warn("设置的数值不可以大于100分！");
            }else{
                return toAjax(preMuIndustryCoefficientService.insertPreMuIndustryCoefficient(preMuIndustryCoefficient));
            }
        }
    }

    /**
     * 修改行业系数
     */
    @RequiresPermissions("premu:coefficient:edit")
    @Log(title = "行业系数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PreMuIndustryCoefficient preMuIndustryCoefficient)
    {
        String district= SecurityUtils.getLoginUser().getSysUser().getDistrict();

            preMuIndustryCoefficient.setCounty(district);
            double totle=0;
            if(preMuIndustryCoefficient.getPreMuIndustryValuesList()!=null){

                for (int i = 0; i <preMuIndustryCoefficient.getPreMuIndustryValuesList().size() ; i++) {
                    totle+=Double.valueOf(preMuIndustryCoefficient.getPreMuIndustryValuesList().get(i).getIndicatorValues());
                }
            }
            if(totle>100){
                return warn("设置的数值不可以大于100分！");
            }else{
            return toAjax(preMuIndustryCoefficientService.updatePreMuIndustryCoefficient(preMuIndustryCoefficient));
        }
    }

    /**
     * 删除行业系数
     */
    @RequiresPermissions("premu:coefficient:remove")
    @Log(title = "行业系数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(preMuIndustryCoefficientService.deletePreMuIndustryCoefficientByBaseIds(baseIds));
    }
}
